package com.xyq.fs.index;

import java.io.EOFException;
import java.io.IOException;

import java.nio.file.Path;
import java.nio.file.Paths;


import org.apache.lucene.document.TextField;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;

import com.xyq.fs.base.MyDirectoryS;
import com.xyq.fs.constants.FieldConstant;
import com.xyq.fs.contants.filesuffixes2.PdfEnums;

public class IndexPdfFiles implements IndexFileInf {


	static PDFTextStripper stripper;

	static {
		try {
			stripper = new PDFTextStripper();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public String getContent(Path path, int endNum) throws java.io.EOFException {

		// 结束提取页数
		String content = "";

		try (PDDocument document = PDDocument.load(path.toFile())) {

			stripper.setStartPage(1);
			stripper.setEndPage(endNum);
			content = stripper.getText(document).trim();

		} catch (java.io.EOFException e) {
			//System.out.println("无法解析2" + path);
		} catch (Exception e) {
			//System.out.println("无法解析" + path);
		} finally {
			path = null;
		}
		if (content.length() == 0 && endNum == 10)
			content = "抱歉,文件暂时无法预览";
		return content.trim();
	}

	@Override
	public void indexFile(Path path, String bz) {

		Document doc = getSimDoc(path, PdfEnums.PDF, bz);
		// 开始提取页数
		// 结束提取页数
		String content = "";
		try {
			content = getContent(path, 20);
		} catch (java.io.EOFException e) {
		//	System.out.println("无法解析2" + path);
		} catch (Exception e) {

			//System.out.println("无法解析" + path);
		} finally {
			
			TextField tf = new TextField(FieldConstant.FILE_CONTENT, content, Store.NO);
			doc.add(tf);
			MyDirectoryS.addDocument(doc);
			
			doc.clear();
			doc = null;
			tf = null;
			content = null;
		}
	}

	public static void main(String[] args) throws EOFException {

		IndexPdfFiles ipd = new IndexPdfFiles();
		String str = ipd.getContent(Paths.get("d:\\中赣盐党办发[2015]4号.pdf"), 15);
		System.out.println("输出"+str);
	}
}
